/* GUI CONVERTED from ppptmtd.p (converter v1.76) Mon Oct 14 12:58:56 2002 */
/* ppptmtd.p - ITEM COST MAINTENANCE SUBPROGRAM                         */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.35 $                                                    */
/*                                                                      */
/* REVISION: 7.0      LAST MODIFIED: 09/04/91   BY: pma *F003*          */
/* REVISION: 7.0      LAST MODIFIED: 03/12/92   BY: WUG *F281*          */
/* REVISION: 7.0      LAST MODIFIED: 06/25/92   BY: emb *F687*          */
/* REVISION: 7.0      LAST MODIFIED: 07/25/92   BY: pma *F782*          */
/* REVISION: 7.0      LAST MODIFIED: 08/17/92   BY: pma *F857*          */
/* REVISION: 7.3      LAST MODIFIED: 01/26/93   BY: bcm *G429*          */
/* REVISION: 7.3      LAST MODIFIED: 02/05/93   BY: pma *G032*          */
/*                                   10/12/93   by: jzs *GG27*          */
/* REVISION: 7.3      LAST MODIFIED: 11/02/93   BY: ais *GG86*          */
/* REVISION: 7.3      LAST MODIFIED: 11/22/93   BY: ais *GH31*          */
/* REVISION: 7.3      LAST MODIFIED: 04/29/94   BY: ais *GJ67*          */
/*           7.3                     09/03/94   BY: bcm *GL93*          */
/*           7.3                     09/11/94   BY: rmh *GM19*          */
/*           7.3                     09/20/94   BY: ljm *GM74*          */
/* REVISION: 7.0      LAST MODIFIED: 10/17/94   BY: ais *FS49*          */
/* REVISION: 7.3      LAST MODIFIED: 11/07/94   BY: ame *GO27*          */
/* REVISION: 7.3      LAST MODIFIED: 11/11/94   BY: ljm *GO05*          */
/* REVISION: 7.3      LAST MODIFIED: 01/28/95   BY: ljm *G0D7*          */
/* REVISION: 7.3      LAST MODIFIED: 03/03/95   BY: qzl *G0G9*          */
/* REVISION: 7.3      LAST MODIFIED: 03/30/95   BY: srk *G0JP*          */
/*           7.4                     07/13/95   BY: jzs *G0S6*          */
/*           7.4                     11/02/95   BY: str *G1B0*          */
/*           7.3                     11/27/95   BY: dzn *G1F0*          */
/* REVISION: 8.5      LAST MODIFIED: 08/21/95   BY: wep *J054*          */
/* REVISION: 8.6      LAST MODIFIED: 10/10/96   BY: svs *K007*          */
/* REVISION: 8.6      LAST MODIFIED: 10/18/96   BY: jpm *K017*          */
/* REVISION: 8.6      LAST MODIFIED: 04/08/97   BY: Markus Barone *H0WT**/
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane    */
/* REVISION: 8.6E     LAST MODIFIED: 04/21/98   BY: *J2J8* Niranjan R.  */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan   */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan   */
/* REVISION: 9.0      LAST MODIFIED: 11/20/98   BY: *M002* Mayse Lai    */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan   */
/* Old ECO marker removed, but no ECO header exists *F0PN*                  */
/* Revision: 1.18     BY: G.Latha           DATE: 03/03/00   ECO: *N03T*    */
/* Revision: 1.19     BY: Annasaheb Rahane  DATE: 05/08/00   ECO: *N0B0*    */
/* Revision: 1.20     BY: Zheng Huang       DATE: 06/09/00   ECO: *N0D3*    */
/* Revision: 1.24     BY: G.Latha           DATE: 05/16/00   ECO: *N0B9*    */
/* Revision: 1.26     BY: Zheng Huang       DATE: 07/18/00   ECO: *N0DK*    */
/* Revision: 1.28     BY: Mark Brown        DATE: 08/13/00   ECO: *N0KQ*    */
/* Revision: 1.29     BY: Vandna Rohira     DATE: 08/14/00   ECO: *M0QG*    */
/* Revision: 1.30     BY: Katie Hilbert     DATE: 04/01/01   ECO: *P002*    */
/* Revision: 1.31     BY: Kirti Desai       DATE: 10/31/01   ECO: *M1P7*    */
/* Revision: 1.32     BY: Jean Miller       DATE: 04/03/02   ECO: *P053*    */
/* Revision: 1.33     BY: Zheng Huang       DATE: 01/31/02 ECO: *P000*      */
/* Revision: 1.34     BY: John Pison        DATE: 04/30/02  ECO: *N1HN*     */
/* $Revision: 1.35 $  BY: John Pison    DATE: 10/11/02  ECO: *N1WX*        */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*                                                                          */
/*V8:ConvertMode=Maintenance                                                */

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */
{pxmaint.i}
{pxpgmmgr.i}

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE ppptmtd_p_1 "Pri"
/* MaxLen: Comment: */

&SCOPED-DEFINE ppptmtd_p_2 "Category"
/* MaxLen: Comment: */

&SCOPED-DEFINE ppptmtd_p_4 "Lower Level"
/* MaxLen: Comment: */

&SCOPED-DEFINE ppptmtd_p_5 "A/O"
/* MaxLen: Comment: */

&SCOPED-DEFINE ppptmtd_p_11 "Element"
/* MaxLen: Comment: */

&SCOPED-DEFINE ppptmtd_p_13 "Total"
/* MaxLen: Comment: */

&SCOPED-DEFINE ppptmtd_p_15 "This Level"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

define variable dflag as integer.
define variable i as integer.

define shared variable promo_old like pt_promo.
define shared frame d.
define shared frame d1.

define shared frame d0.
define shared variable ppform as character.
define shared variable frtitle as character format "x(24)".
define shared variable csset like cs_set initial "".

define shared variable site like si_site initial "".

/* Define shared variable global_costsim like sc_sim. */
define shared variable global_category like sc_category.
define new shared variable global_element like sc_element.
define shared variable startrow as integer.
define shared variable transtype as character.
define variable del as character format "x(15)".
define variable usr_val as character format "x(12)" extent 5.
define variable usr_elm like sc_element extent 5.
define variable yn like mfc_logical.
define variable seq2 as decimal.
define variable sw_reset like mfc_logical no-undo.
define variable sw_key as character initial ? no-undo.
define shared workfile sptwkfl
   fields element  like spt_element column-label {&ppptmtd_p_11}
   fields primary  like mfc_logical column-label {&ppptmtd_p_1}
   fields prim2    like mfc_logical
   fields ao       like spt_ao      column-label {&ppptmtd_p_5}
   fields cst_tl   like spt_cst_tl  column-label {&ppptmtd_p_15}
   fields old_cst_tl like spt_cst_tl
   fields cst_ll   like spt_cst_ll  column-label {&ppptmtd_p_4}
   fields cst_tot  like spt_cst_tl  column-label {&ppptmtd_p_13}
   fields cat_desc as character     column-label {&ppptmtd_p_2}
   fields seq      like spt_pct_ll
   fields part     like pt_part.
define buffer sptwkfl2 for sptwkfl.

define variable oldcst like sct_cst_tot no-undo.

define variable prev_cst_tl like spt_cst_tl.
define variable new_cst_tl like spt_cst_tl.
define variable upd_cst_date as logical no-undo.
define variable apm-ex-prg as character format "x(10)" no-undo.
define variable apm-ex-sub as character format "x(24)" no-undo.

define variable error_flag  like mfc_logical           no-undo.
define variable err_mess_no like msg_nbr               no-undo.
define variable v_std_cost  like sct_cst_tot           no-undo.
define variable v_std_cost_set  like sct_sim           no-undo.

define variable desktopLastKey as character no-undo.
define variable savedStatus as character no-undo.

del = getTermLabel("DELETED",15). /* ASSIGN INITIAL VALUE */

/*  SPT_PCT_LL IS USED TO INDICATE THE CATEGORY OF THE ELEMENT AS  */
/*  WELL AS WHETHER IT IS PRIMARY OR NOT.  FOR A PRIMARY ELEMENT   */
/*  SPT_PCT_LL WILL = x.00; FOR A NON-PRIMARY IT MUST EQUAL x.01.  */
/*  IT MUST BE PROGRAMATICALLY ENFORCED THAT THERE IS ONE AND ONLY */
/*  ONLY ONE PRIMARY ELEMENT FOR EACH CATEGORY!!!!!                */

{gptxcdec.i}    /* DECLARATIONS FOR gptxcval.i */
{gpglefdf.i}    /* DECLARATIONS FOR gpglef.i   */

{pxsevcon.i}

/*CHANGED ALL OCCURANCES OF SISITE TO SITE*/

for each lngd_det no-lock where lngd_dataset = "sc_mstr"
      and lngd_key1 = ""
      and lngd_key2 = ""
      and lngd_key3 = ""
      and lngd_field = "sc_category"
      and lngd_lang = global_user_lang:
   usr_val[integer(lngd_key4)] = lngd_translation.
   usr_elm[integer(lngd_key4)] = substring(lngd_translation,1,8).
end. /* for each lngd_det no-lock where lngd_dataset = "sc_mstr" */

/*V8:EditableDownFrame=d*/
FORM /*GUI*/ 
   {ppptmta9.i}
with frame d ? down title color normal frtitle width 80 no-attr-space THREE-D /*GUI*/.


/* SET EXTERNAL LABELS */
setFrameLabels(frame d:handle).

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1.4 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1   COLUMN 3 NO-LABEL
 SKIP(.1)  /*GUI*/
{ppptmta9.i}
 SKIP(.4)  /*GUI*/
with frame d0 1 down width 80 overlay row startrow no-labels
    no-attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-d0-title AS CHARACTER.
 F-d0-title = (getFrameTitle("TOTALS",16)).
 RECT-FRAME-LABEL:SCREEN-VALUE in frame d0 = F-d0-title.
 RECT-FRAME-LABEL:WIDTH-PIXELS in frame d0 =
  FONT-TABLE:GET-TEXT-WIDTH-PIXELS(
  RECT-FRAME-LABEL:SCREEN-VALUE in frame d0 + " ", RECT-FRAME-LABEL:FONT).
 RECT-FRAME:HEIGHT-PIXELS in frame d0 =
  FRAME d0:HEIGHT-PIXELS - RECT-FRAME:Y in frame d0 - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME d0 = FRAME d0:WIDTH-CHARS - .5. /*GUI*/


FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1.4 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1   COLUMN 3 NO-LABEL
 SKIP(.1)  /*GUI*/
{ppptmta5.i}
 SKIP(.4)  /*GUI*/
with frame d1 
   side-labels width 80 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-d1-title AS CHARACTER.
 F-d1-title = (getFrameTitle("ITEM_PRICE_DATA",23)).
 RECT-FRAME-LABEL:SCREEN-VALUE in frame d1 = F-d1-title.
 RECT-FRAME-LABEL:WIDTH-PIXELS in frame d1 =
  FONT-TABLE:GET-TEXT-WIDTH-PIXELS(
  RECT-FRAME-LABEL:SCREEN-VALUE in frame d1 + " ", RECT-FRAME-LABEL:FONT).
 RECT-FRAME:HEIGHT-PIXELS in frame d1 =
  FRAME d1:HEIGHT-PIXELS - RECT-FRAME:Y in frame d1 - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME d1 = FRAME d1:WIDTH-CHARS - .5. /*GUI*/


/* SET EXTERNAL LABELS */
setFrameLabels(frame d1:handle).

dflag = 0.

i = maximum(0,15 - length(del)).
del = fill(" ",i) + del.

find first soc_ctrl no-lock no-error.

alphaloop:
do on error undo, leave:
/*GUI*/ if global-beam-me-up then undo, leave.


   do transaction:
      find pt_mstr exclusive-lock where recid(pt_mstr) = pt_recno.
   end. /* do transaction: */

   if ppform = "" or ppform = "d" then do:
      loopd1:
      do transaction with frame d1 on endkey undo, leave alphaloop:
/*GUI*/ if global-beam-me-up then undo, leave.


         view frame d1.
         view frame d.
         ststatus = stline[3].
         status input ststatus.
         display pt_price pt_taxable pt_taxc with frame d1.

         setd1:
         do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

            set pt_price pt_taxable pt_taxc with frame d1.

            {pxrun.i &PROC       = 'validateTaxInfo' &PROGRAM = 'ppitxr.p'
               &PARAM      = "(input pt_taxc)"
               &NOAPPERROR = true
               &CATCHERROR = true
               }

            if return-value <> {&SUCCESS-RESULT} then do:
               next-prompt pt_taxc with frame d1.
               undo, retry.
            end. /* if return-value <> {&SUCCESS-RESULT} then do: */
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /*setd1*/

         site = pt_site.
         /*@MODULE APM BEGIN*/
         if soc_apm and
            (promo_old <> "" or pt_promo <> "") then do:
            /* Future logic will go here to determine subdirectory*/

            apm-ex-sub = "if/".

            /* RUN INTERNAL PROCEDURE TO OBTAIN COST SET AND COST */
            for first si_mstr
               fields (si_cur_set si_entity si_gl_set si_site)
               where si_site = pt_site no-lock:
            end. /* for first si_mstr */
            run get_std_cost
               (input  pt_part,
               input  si_site,
               output v_std_cost,
               output v_std_cost_set).

            /* UPDATE GENERIC ITEM RECORD IN APM */
            {gprunex.i
               &module   = 'APM'
               &subdir   = apm-ex-sub
               &program  = 'ifapm054.p'
               &params   = "(input  pt_part,
                 input  pt_desc1,
                 input  pt_desc2,
                 input  pt_net_wt,
                 input  pt_net_wt_um,
                 input  pt_price,
                 input  pt_promo,
                 input  pt_site,
                 input  pt_taxc,
                 input  pt_um,
                 input  v_std_cost,
                 input  v_std_cost_set,
                 input  pt_pm_code,
                 output error_flag,
                 output err_mess_no)"}

            if error_flag then do:
               {pxmsg.i &MSGNUM=err_mess_no &ERRORLEVEL={&APP-ERROR-RESULT}}
               /* ERROR RETURNED BY IFAPM054.P */
               undo, return.
            end. /* if error_flag then do: */
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* (promo_old <> "" or pt_promo <> "") then do: */
         /*@MODULE APM END*/
      end. /* do transaction with frame d1 on endkey undo, leave alphaloop: */
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* if ppform = "" or ppform = "d"*/

   mainloop:
   repeat with frame d:
/*GUI*/ if global-beam-me-up then undo, leave.


      if batchrun
         and not transtype begins "utcost"
         then leave.
      if dflag < 0 then return.
      find first icc_ctrl no-lock.

      {pxrun.i &PROC       = 'processRead' &PROGRAM = 'icsixr.p'
         &PARAM      = "(input  site,
           buffer si_mstr,
           input  {&NO_LOCK_FLAG},
           input  {&NO_WAIT_FLAG})"
         &NOAPPERROR = true
         &CATCHERROR = true
         }

      /* CHANGED FOURTH PARAMETER FROM LOCK_FLAG TO NO_LOCK_FLAG */
      /* CHANGED FIFTH  PARAMETER FROM WAIT_FLAG TO NO_WAIT_FLAG */

      {pxrun.i &PROC       = 'processRead' &PROGRAM = 'icinxr.p'
         &PARAM      = "(input  pt_part,
           input  site,
           buffer in_mstr,
           input  {&NO_LOCK_FLAG},
           input  {&NO_WAIT_FLAG})"
         &NOAPPERROR = true
         &CATCHERROR = true
         }
      if return-value = {&SUCCESS-RESULT} then do:

         /*DE-COUPLE PROGRAMS FOR OBJECTS*/
         {pxrun.i &PROC       = 'updateCostSets' &PROGRAM = 'icinxr.p'
            &PARAM      = "(buffer in_mstr,
              buffer si_mstr)"
            &NOAPPERROR = true
            &CATCHERROR = true
            }
      end. /* if return-value = {&SUCCESS-RESULT} then do: */

      /* PPFORM IS d3 IN 30.13.5, 30.17.5 AND 1.4.15 */
      /* LINKED COSTS ARE NOT USED IN THOSE MENU ITEMS */
      if ppform = "d3"
         then do transaction on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.


         {pxrun.i &PROC       = 'findByKey' &PROGRAM = 'ppicxr.p'
            &PARAM      = "(input  csset,
              input  pt_part,
              input  site,
              input  false,
              buffer sct_det)"
            &NOAPPERROR = true
            &CATCHERROR = true
            }

         dflag = -1.
      end. /* repeat with frame d: */
      else do transaction on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.


         if dflag = 0 then do:

            if si_gl_set = "" then csset = icc_gl_set.
            else csset = si_gl_set.
            if si_site <> in_gl_cost_site then do:
               for first si_mstr fields(si_gl_set)
                  where si_site = in_gl_cost_site no-lock:
               end.
               if available si_mstr then
                  if si_gl_set = "" then csset = icc_gl_set.
               else csset = si_gl_set.
               /* FIND PREVIOUS RECORD SO REST OF CODE WILL WORK */
               find si_mstr where si_site = site no-lock no-error.
            end.

            {pxrun.i &PROC       = 'findByKey' &PROGRAM = 'ppicxr.p'
               &PARAM      = "(input  csset,
                 input  pt_part,
                 input  site,
                 input  true,
                 buffer sct_det)"
               &NOAPPERROR = true
               &CATCHERROR = true
               }

            frtitle = getFrameTitle("GENERAL_LEDGER_COST_DATA",16).

            if pt_site <> "" then

            frtitle = frtitle + "(" + getTermLabel("SITE",8) + ": "
            + in_mstr.in_gl_cost_site
            + " / " + getTermLabel("COST_SET",6) + ": " + csset + ") ".

         end. /*if dflag = 0*/

         else if dflag = 1 then do:

            if si_cur_set = "" then csset = icc_cur_set.
            else csset = si_cur_set.

            {pxrun.i &PROC       = 'findByKey' &PROGRAM = 'ppicxr.p'
               &PARAM      = "(input  csset,
                 input  pt_part,
                 input  site,
                 input  true,
                 buffer sct_det)"
               &NOAPPERROR = true
               &CATCHERROR = true
               }

            frtitle = getFrameTitle("CURRENT_COST_DATA",25).

            if pt_site <> "" then

            frtitle = frtitle + "(" + getTermLabel("SITE",8) + ": " + site
            + " / " + getTermLabel("COST_SET",6) + ": " + csset + ") ".

            dflag = -1.
            sw_key = ?.
         end. /*if dflag = 1*/
         else do:
            return.
         end. /* else do: */
         if dflag = 0 then dflag = 1.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /*else do (ppform <> d3)*/

      {pxrun.i &PROC       = 'processRead' &PROGRAM = 'cscsxr.p'
         &PARAM      = "(input  csset,
           buffer cs_mstr,
           input  {&NO_LOCK_FLAG},
           input  {&NO_WAIT_FLAG})"
         &NOAPPERROR = true
         &CATCHERROR = true
         }

      for each sptwkfl exclusive-lock:
         delete sptwkfl.
      end. /* for each sptwkfl exclusive-lock: */

      for each sptwkfl2 exclusive-lock:
         delete sptwkfl2.
      end. /* for each sptwkfl2 exclusive-lock: */

      for each spt_det no-lock where spt_site = sct_site
            and spt_sim  = sct_sim
            and spt_part = sct_part
            by spt_pct_ll by spt_cst_tl descending by spt_element:
         create sptwkfl.
         assign element    = spt_element
            primary    = no
            prim2      = no
            ao         = spt_ao
            cst_tl     = spt_cst_tl
            old_cst_tl = spt_cst_tl
            cst_ll     = spt_cst_ll
            cst_tot    = spt_cst_tl + spt_cst_ll
            part       = sct_part
            seq        = truncate(spt_pct_ll,0).

         if recid(sptwkfl) = -1 then .
         if spt_pct_ll = truncate(spt_pct_ll,0) then do:
            assign
               primary = yes
               prim2   = yes.
         end. /* if spt_pct_ll = truncate(spt_pct_ll,0) then do: */
         if transtype = "UTCOSTZ" then cst_tl = 0.
         cat_desc = usr_val[integer(truncate(seq,0))].
      end. /* for each spt_det no-lock where spt_site = sct_site */

      find first sptwkfl no-error.
      if not available sptwkfl then leave.

      if ppform <> "d2" then
         pause 0 no-message.
      hide frame d0 no-pause.
      hide frame d no-pause.
      view frame d0.
      view frame d.

      clear frame d0 no-pause.
/*GUI*/ RECT-FRAME-LABEL:SCREEN-VALUE in frame d0 = F-d0-title.
      clear frame d all no-pause.

      /*Swallow up extra "GO"*/
      if {gpiswrap.i} and keyfunction(lastkey) = "GO" then do:
         readkey pause 0.
         if keyfunction(lastkey) <> "GO" then
            apply lastkey.
      end.

      loopd:

      repeat transaction with frame d on endkey undo mainloop,
            leave alphaloop:
/*GUI*/ if global-beam-me-up then undo, leave.


         ststatus = stline[3].
         status input ststatus.

         oldcst = sct_cst_tot.

         global_element = fill("#",8).

         if transtype begins "UTCOST" then do:
            utloop:
            repeat with frame d:
/*GUI*/ if global-beam-me-up then undo, leave.

               pause 0.
               /* WE ONLY SKIP THIS IF IT IS FOR GL COST (dflag = 1) */
               /* AT A TARGET SITE(ALSO CALLED AS INVENTORY SITE) .  */
               if (dflag <> 1
                  or (available in_mstr and in_site = in_gl_cost_site)) then
               do:
                  create sptwkfl.

                  if recid(sptwkfl) = -1 then .
                  prev_cst_tl = cst_tl.

                  /* CHECK SITE SECURITY FOR THE USER GROUP */
                  {pxrun.i &PROC  ='validateSiteSecurity' &PROGRAM ='icsixr.p'
                     &PARAM ="(input site,
                       input {&SUPPRESS-MSG})"
                     &NOAPPERROR=true
                     &CATCHERROR=true}

                  if return-value <> {&SUCCESS-RESULT} then do:
                     /* USER DOES NOT HAVE ACCESS TO DEFAULT SITE */
                     {pxmsg.i &MSGNUM=2711
                        &ERRORLEVEL={&APP-ERROR-NO-REENTER-RESULT}
                        &MSGARG1=site}
                     /*V8+*/
                     leave utloop.
                  end. /* IF RETURN_VALUE <> (&SUCCESS-RESULT} */

                  set element cst_tl with frame d.

                  if element = "" then leave utloop.
                  /* SECTION ADDED TO VERIFY GL IS OPEN */
                  assign
                     new_cst_tl = cst_tl
                     cst_tl     = prev_cst_tl.
                  if new_cst_tl <> prev_cst_tl
                     and dflag = 1
                  then do:

                     {pxrun.i &PROC='validateDateInGLPeriod' &PROGRAM ='glglxr.p'
                        &PARAM      = "(input site,
                          input ""IC"",
                          input today)"
                        &NOAPPERROR = true
                        &CATCHERROR = true
                        }
                     if return-value <> {&SUCCESS-RESULT} then do:
                        undo loopd, retry.
                     end. /* if return-value <> {&SUCCESS-RESULT} */

                  end. /* then do: */
                  cst_tl = new_cst_tl.
                  /* END OF CODE ADDED TO VERIFY GL IS OPEN */

                  {pxrun.i &PROC       = 'processRead' &PROGRAM ='sccexr.p'
                     &PARAM      = "(input  sct_sim,
                       input  element,
                       buffer sc_mstr,
                       input  {&LOCK_FLAG},
                       input  {&NO_WAIT_FLAG})"
                     &NOAPPERROR = true
                     &CATCHERROR = true
                     }

                  if return-value <> {&SUCCESS-RESULT} then do:
                     {pxmsg.i &MSGNUM=103 &ERRORLEVEL={&APP-ERROR-RESULT}}
                     undo utloop, retry.
                  end. /* if not available sc_mstr then do: */
               end. /* do: */
               do for sptwkfl2:
                  find first sptwkfl2
                     where sptwkfl2.element = sptwkfl.element
                     and recid(sptwkfl2) <> recid(sptwkfl) no-error.
                  if not available sptwkfl2 then do:
                     create sptwkfl2.
                     assign sptwkfl2.element = sptwkfl.element
                        sptwkfl2.primary = no
                        sptwkfl2.prim2 = no
                        sptwkfl2.ao = no
                        sptwkfl2.cst_tl = 0
                        sptwkfl2.cst_ll = 0
                        sptwkfl2.cst_tot = 0
                        sptwkfl2.part = sct_part
                        sptwkfl2.seq = integer(sc_category).
                     sptwkfl2.cat_desc =
                     usr_val[integer(sptwkfl2.seq)].

                     if recid(sptwkfl2) = -1 then .
                  end. /* if not available sptwkfl2 then do: */
                  sptwkfl2.cst_tl = sptwkfl.cst_tl.
                  cst_tot = cst_tl + cst_ll.

                  delete sptwkfl.
               end. /* do for sptwkfl2: */
               down.
            end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* repeat with frame d: */
         end. /* if transtype begins "UTCOST" then do: */

         else do:
            for each sptwkfl break by part:
               accumulate cst_tl (total by part).
               accumulate cst_ll (total by part).
               accumulate cst_tot (total by part).
            end. /* for each sptwkfl break by part: */

            display (getTermLabel("TOTALS",7) + ":") @ element
               (accumulate total cst_tl) @ cst_tl
               (accumulate total cst_ll) @ cst_ll
               (accumulate total cst_tot) @ cst_tot
               sct_cst_date @ cat_desc with frame d0.

            /*Need ststatus to have delete option for Desktop 2*/
            if {gpiswrap.i} then do:
               assign
                  savedStatus = ststatus
                  ststatus = stline[2].
               status input ststatus.
            end.

            {csscrad1.i sptwkfl " " element
               "element cst_tl when (seq <> 9999)
                 del when (seq = 9999) @ cst_tl
                 primary cst_ll cst_tot ao cat_desc"
               element d "yes" }

            if {gpiswrap.i} then do:
               ststatus = savedStatus.
               status input ststatus.
            end.

            /*Swallow up extra "RETURN"*/
            if {gpiswrap.i} and desktopLastkey = "insert" then do:
               if keyfunction(lastkey) = "return" then do:
                  desktopLastkey = "".
                  readkey pause 0.
                  apply lastkey.
               end.
            end.

            if frame-value = ""
               then global_element = "".
            else global_element = fill("#",8).

            if false then do:
               update element validate (true,"") with frame d.
            end. /* if false then do: */

            /*IF DELETE-UNDELETE IS PRESSED*/

            if (lastkey = keycode("F5")
               /*V8+*/
                    
               or keyfunction(lastkey) = "delete-character"   
               )
               /* WE ONLY SKIP THIS IF IT IS FOR GL COST (dflag = 1) */
               /* AT A TARGET SITE.                                  */
               and (dflag <> 1
               or (available in_mstr and in_site = in_gl_cost_site))
            then do:
               /* Validate delete and mark line as deleted*/
               {ppptmtd.i}
            end. /* then do: */

            /*NEW ELEMENT ADDED*/
            if (keyfunction(lastkey) = "insert-mode"
               or lastkey = keycode("CTRL-A")
               or (keyfunction(lastkey) = "return" and frame-value = ""))
               /* WE ONLY SKIP THIS IF IT IS FOR GL COST (dflag = 1) */
               /* AT A TARGET SITE.                                  */
               and (dflag <> 1
               or (available in_mstr and in_site = in_gl_cost_site))
            then do:
               do:
                  create sptwkfl.

                  if recid(sptwkfl) = -1 then .

                  /* CHECK SITE SECURITY FOR THE USER GROUP */
                  {pxrun.i &PROC  ='validateSiteSecurity' &PROGRAM ='icsixr.p'
                     &PARAM ="(input site,
                       input {&SUPPRESS-MSG})"
                     &NOAPPERROR=true
                     &CATCHERROR=true}

                  if return-value <> {&SUCCESS-RESULT} then do:
                     /* USER DOES NOT HAVE ACCESS TO DEFAULT SITE */
                     {pxmsg.i &MSGNUM=2711
                        &ERRORLEVEL={&APP-ERROR-NO-REENTER-RESULT}
                        &MSGARG1=site}
                     /*V8+*/
                     leave loopd.
                  end. /* IF RETURN_VALUE <> (&SUCCESS-RESULT} */

                  if {gpiswrap.i} then desktopLastkey = "insert".

                  set element with frame d.

                  if {gpiswrap.i} then desktopLastkey = "return".

                  {pxrun.i &PROC       = 'processRead' &PROGRAM ='sccexr.p'
                     &PARAM      = "(input  sct_sim,
                       input  element,
                       buffer sc_mstr,
                       input  {&LOCK_FLAG},
                       input  {&NO_WAIT_FLAG})"
                     &NOAPPERROR = true
                     &CATCHERROR = true
                     }

                  if return-value <> {&SUCCESS-RESULT} then do:
                     {pxmsg.i &MSGNUM=103 &ERRORLEVEL={&APP-ERROR-RESULT}}
                     pause.
                     sw_key = ?.
                     undo loopd, retry.
                  end. /* if not available sc_mstr then do: */
               end. /* do: */
               do for sptwkfl2:
                  find first sptwkfl2
                     where sptwkfl2.element = sptwkfl.element
                     and recid(sptwkfl2) <> recid(sptwkfl) no-error.
                  if not available sptwkfl2 then do:
                     if frame-line(d) = 1 then
                        find first sptwkfl2 no-error.
                     else
                     find last sptwkfl2 where recid(sptwkfl2)
                        = recno24[frame-line(d) - 1] no-error.
                     create sptwkfl2.
                     assign sptwkfl2.element = sptwkfl.element
                        sptwkfl2.primary = no
                        sptwkfl2.prim2 = no
                        sptwkfl2.ao = no
                        sptwkfl2.cst_tl = 0
                        sptwkfl2.cst_ll = 0
                        sptwkfl2.cst_tot = 0
                        sptwkfl2.part = sct_part
                        sptwkfl2.seq = integer(sc_category).
                     sptwkfl2.cat_desc =
                     usr_val[integer(sptwkfl2.seq)].
                     sw_key = ?.

                     if recid(sptwkfl2) = -1 then .
                  end. /* if not available sptwkfl2 then do: */
                  recno = recid(sptwkfl2).

                  delete sptwkfl.
                  find sptwkfl where recid(sptwkfl) = recno.
               end. /* do for sptwkfl2: */
            end. /* then do: */

            if (keyfunction(lastkey) = "return"  or
               ({gpiswrap.i} and desktopLastKey = "return"))
               /* WE ONLY SKIP THIS IF IT IS FOR GL COST (dflag = 1) */
               /* AT A TARGET SITE.                                  */
               and (dflag <> 1 or
               (available in_mstr and in_site = in_gl_cost_site))
               and seq <> 9999 then do:

               if {gpiswrap.i} then desktopLastKey = "".

               display cst_tl cst_ll cst_tot primary cat_desc ao
               with frame d.
               prev_cst_tl = cst_tl.

               /* CHECK SITE SECURITY FOR THE USER GROUP */
               {pxrun.i &PROC  ='validateSiteSecurity' &PROGRAM ='icsixr.p'
                  &PARAM ="(input site,
                    input {&SUPPRESS-MSG})"
                  &NOAPPERROR=true
                  &CATCHERROR=true}

               if return-value <> {&SUCCESS-RESULT} then do:
                  /* USER DOES NOT HAVE ACCESS TO DEFAULT SITE */
                  {pxmsg.i &MSGNUM    = 2711
                     &ERRORLEVEL= {&APP-ERROR-NO-REENTER-RESULT}
                     &MSGARG1   = site}
                  /*V8+*/
                  leave loopd.
               end. /* IF RETURN_VALUE <> (&SUCCESS-RESULT} */

               set cst_tl
                  primary when (not primary)
                  ao when (cs_method = "AVG" and seq = 4)
               go-on(F5 ctrl-d)
               with frame d.

               /* SECTION ADDED TO VERIFY GL IS OPEN */
               assign
                  new_cst_tl = cst_tl
                  cst_tl     = prev_cst_tl.

               if new_cst_tl <> prev_cst_tl
                  and dflag = 1
                  and not transtype begins "SIM"
               then do:

                  {pxrun.i &PROC='validateCurrentCostDate' &PROGRAM='ppicxr1.p'
                     &PARAM = "(input site)"
                     &NOAPPERROR = true
                     &CATCHERROR = true
                     }
                  if return-value <> {&SUCCESS-RESULT} then do:
                     undo loopd, retry.
                  end. /* if return-value <> {&SUCCESS-RESULT} */

               end. /* then do: */
               cst_tl = new_cst_tl.
               /* END OF CODE ADDED TO VERIFY GL IS OPEN */
               cst_tot = cst_tl + cst_ll.

               if {gpiswrap.i} then
                  sw_reset = yes.
               else
                  display cst_tot.

               if primary and not prim2 then do:
                  seq2 = seq.
                  for each sptwkfl where seq = seq2:
                     if primary and prim2 then do:
                        assign
                           primary = no
                           prim2   = no.
                     end. /* if primary and prim2 then do: */
                     else if primary and not prim2 then do:
                        prim2 = yes.
                     end. /* else if primary and not prim2 then do: */
                  end. /* for each sptwkfl where seq = seq2: */
                  sw_key = ?.
               end. /* if primary and not prim2 then do: */
            end. /* and seq <> 9999 then do: */
         end. /*else (transtype begins UTCOST) do*/

         /*ACCEPT DATA*/
         if (keyfunction(lastkey) = "go" and frame-field = "element")
            or (keyfunction(lastkey) = "go" and frame-field = ""
            and recno24[frame-line(d)] = 0)
            or transtype begins "UTCOST" then do:
            yn = true.
            if yn then do:

               upd_cst_date = false.
               for each sptwkfl where element <> "":
/*GUI*/ if global-beam-me-up then undo, leave.

                  if cst_tl <> old_cst_tl then upd_cst_date = true.

                  {pxrun.i &PROC='processRead' &PROGRAM='ppicxr1.p'
                     &PARAM = "(input sct_sim,
                       input element,
                       input sct_part,
                       input sct_site,
                       buffer spt_det,
                       input  {&LOCK_FLAG},
                       input  {&NO_WAIT_FLAG})"
                     &NOAPPERROR = true
                     &CATCHERROR = true
                     }
                  if seq <> 9999 then do:
                     if not available spt_det then do:

                        {pxrun.i &PROC='createItemCostElement' &PROGRAM='ppicxr1.p'
                           &PARAM = "(input sct_part,
                             input sct_site,
                             input sct_sim,
                             input element,
                             buffer spt_det)"
                           &NOAPPERROR = true
                           &CATCHERROR = true
                           }

                        if recid(spt_det) = -1 then .
                     end. /* if not available spt_det then do: */

                     {pxrun.i &PROC       = 'processRead' &PROGRAM ='sccexr.p'
                        &PARAM      = "(input  sct_sim,
                          input  element,
                          buffer sc_mstr,
                          input  {&LOCK_FLAG},
                          input  {&NO_WAIT_FLAG})"
                        &NOAPPERROR = true
                        &CATCHERROR = true
                        }

                     if return-value <> {&SUCCESS-RESULT} then do:
                        {pxmsg.i &MSGNUM=103 &ERRORLEVEL={&APP-ERROR-RESULT}}
                        undo loopd, retry.
                     end. /* if not available sc_mstr then do: */

                     spt_pct_ll = decimal(sc_category) + .01.
                     if primary
                        then spt_pct_ll = truncate(spt_pct_ll,0).
                     spt_cst_tl = cst_tl.
                     spt_ao = ao.
                  end. /* if seq <> 9999 then do: */
                  else if seq = 9999 and available spt_det then do:
                     delete spt_det.
                  end. /* else if seq = 9999 and available spt_det then do: */
               end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* for each sptwkfl where element <> "": */

               {pxrun.i &PROC       = 'calculateTotals' &PROGRAM ='ppicxr.p'
                  &PARAM      = "(buffer sct_det)"
                  &NOAPPERROR = true
                  &CATCHERROR = true
                  }

               if sct_cst_tot <> oldcst
                  or upd_cst_date = true
               then do:
                  sct_cst_date = today.

                  {pxrun.i &PROC     = 'postCostAdjustment' &PROGRAM ='ppicxr.p'
                     &PARAM    = "(input pt_part,
                       input si_site,
                       input oldcst,
                       buffer sct_det)"
                     &NOAPPERROR = true
                     &CATCHERROR = true
                     }

               end. /* if sct_cst_tot <> oldcst */

               /*@MODULE APM BEGIN*/
               if soc_apm and
                  (promo_old <> "" or pt_promo <> "") then do:
                  /* Future logic will go here to determine subdirectory*/

                  apm-ex-sub = "if/".

                  /* RUN INTERNAL PROCEDURE TO OBTAIN COST SET AND COST */
                  for first si_mstr
                     fields (si_cur_set si_entity si_gl_set si_site)
                     where si_site = pt_site no-lock:
                  end. /* for first si_mstr */
                  run get_std_cost
                     (input  pt_part,
                     input  si_site,
                     output v_std_cost,
                     output v_std_cost_set).

                  /* UPDATE GENERIC ITEM RECORD IN APM */
                  {gprunex.i
                     &module   = 'APM'
                     &subdir   = apm-ex-sub
                     &program  = 'ifapm054.p'
                     &params   = "(input  pt_part,
                       input  pt_desc1,
                       input  pt_desc2,
                       input  pt_net_wt,
                       input  pt_net_wt_um,
                       input  pt_price,
                       input  pt_promo,
                       input  pt_site,
                       input  pt_taxc,
                       input  pt_um,
                       input  v_std_cost,
                       input  v_std_cost_set,
                       input  pt_pm_code,
                       output error_flag,
                       output err_mess_no)"}

                  if error_flag then do:
                     {pxmsg.i &MSGNUM=err_mess_no &ERRORLEVEL={&APP-ERROR-RESULT}}
                     /* ERROR RETURNED BY IFAPM054.P */
                     undo, return.
                  end. /* if error_flag then do: */

               end. /* (promo_old <> "" or pt_promo <> "") then do: */
               /*@MODULE APM END*/

               leave loopd.
            end. /*if yn*/
            else next loopd.
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /*if keyfunction = go*/

         if keyfunction(lastkey) = "end-error" then do:
            dflag = -1.
            leave.
         end. /* if keyfunction(lastkey) = "end-error" then do: */
         if transtype begins "UTCOST" then leave.
      end. /* END LOOP D */

      clear frame d0 no-pause.
/*GUI*/ RECT-FRAME-LABEL:SCREEN-VALUE in frame d0 = F-d0-title.
      clear frame d all no-pause.
      sw_reset = yes.
   end.  /*mainloop*/
end. /*alphaloop*/

{pppstdcs.i} /* INTERNAL PROCEDURE TO OBTAIN COST SET AND COST */

/*MAIN-END*/
